package networkInterface;

import java.awt.Color;
import exception.NoConnectionPossibleException;

import object.Node;

public abstract class WifiInterfaceBase extends NetworkInterfaceBase {
	
	protected int maxConnections = 1;
	protected double range = 0;
	
	public WifiInterfaceBase(Node node, Color c) {
		super(c, node);
	}

	@Override
	public void addConnection(NetworkInterfaceBase netInt) throws NoConnectionPossibleException{
		String ret = canConnectWithReason(netInt);
		if(ret != null)
			throw new NoConnectionPossibleException(ret);
		
		WifiInterfaceBase n = (WifiInterfaceBase)netInt;

		object.Connection c = new object.Connection();
		c.setInterfaces(this, n);
		connections.add(c);
		n.addConnectionWithoutCheck(c);
	}
	
	@Override
	public String canConnectWithReason(NetworkInterfaceBase netInt){
		if(!(netInt instanceof WifiInterfaceBase))
			return "Not a NetworkInterfaceBase";
		
		WifiInterfaceBase n1 = this;
		WifiInterfaceBase n2 = (WifiInterfaceBase)netInt;
		
		for(int x = 0; x <= 1; x++){
			if(x==1){
				n1 = (WifiInterfaceBase)netInt;
				n2 = this;
			}
			
			if(n1.getNode()==null)
				return "Interface doesn't have node";
			
			if(n1.isDirectlyConnected(n2))
				return "Already connected on this interface";
			
			if(n1.getNode()==n2.getNode())
				return "Can't connect to interfaces on same node";
			
			if(n1.getNode().isDirectlyConnected(n2.getNode()))
				return "Nodes already connected on other interface";
			
			if(n1.getNode().getLength(n2.getNode()) > range || n2.getNode().getLength(n1.getNode()) > n2.range)
				return "Out of Range";
			
			if(!n1.hasConnectionsAvailible())
				return "No connections left";
		}
		
		return null;
	}
}
